var shell = require('shelljs');
var chalk = require('chalk');

const error = chalk.bold.red;
const info = chalk.bold.cyan;
const warning = chalk.bold.yellow;

// 默认项目名和环境参数不需要传参数名，其他参数需要传参数名
let otherParams = {};
const neededParams = [];
params = process.argv.splice(2);
params.map(item => {
  if (item.includes('=')) {
    const temparray = item.split('=');
    otherParams[temparray[0]] = temparray[1];
  } else {
    neededParams.push(item);
  }
});
const envname = neededParams && neededParams[0];
console.log(info(neededParams));

let env = 'prod';
if (!envname) {
  console.log(warning('未输入环境，默认为线上（prod环境）'));
}
env = envname || env;

// 设置NODE_ENV被开发环境，用于区分是run dev的开发环境还是 run build的产品环境
process.env.NODE_ENV = 'production';

const ymmUploadENV = {
  dev: 'dev',
  qa: 'beta',
  prod: 'release',
};
const ENVSuffix = {
  MD: 'md', // 基础设施测试环境
  JR: 'jr', // 金融环境
  GA: 'ga', // 贵安 暂时存放在 金融项目下
};

function getFolderName(envSuffix) {
  let folderSuffix = '';

  switch (envSuffix) {
    case ENVSuffix.MD: {
      folderSuffix = ENVSuffix.MD;
      break;
    }
    case ENVSuffix.GA:
    case ENVSuffix.JR: {
      folderSuffix = ENVSuffix.JR;
      break;
    }
    default: {
      folderSuffix = '';
    }
  }

  return `hubble-ui${folderSuffix ? `-${folderSuffix}` : ''}`;
}

const envInfo = env.split('-');
const ymmENV = ymmUploadENV[envInfo[0]];

const folderName = getFolderName(envInfo[envInfo.length - 1]);
// const config = require('./webpack.dev.config.js');
const runCommand = `cross-env BACK_END_ENV=${env} PUBLIC_PATH=${otherParams.publicPath ||
  './'} webpack --config config/webpack.prod.js`;
console.log(info('\n NOW RUNNING:\t' + runCommand + '\n'));
shell.exec(runCommand, function(code, stdout, stderr) {
  if (stderr) {
    console.log(error(stderr));
  }
});
